/**
 * 根据数字获取对应的汉字
 * @param num - 数字(0-10)
 */
export function getHanByNumber(num: number) {
  const HAN_STR = '零一二三四五六七八九十';
  return HAN_STR.charAt(num);
}

/**
 * 将总秒数转换成 分：秒
 * @param seconds - 秒
 */
export function transformToTimeCountDown(seconds: number) {
  const SECONDS_A_MINUTE = 60;
  function fillZero(num: number) {
    return num.toString().padStart(2, '0');
  }
  const minuteNum = Math.floor(seconds / SECONDS_A_MINUTE);
  const minute = fillZero(minuteNum);
  const second = fillZero(seconds - minuteNum * SECONDS_A_MINUTE);
  return `${minute}: ${second}`;
}

/**
 * 获取指定整数范围内的随机整数
 * @param start - 开始范围
 * @param end - 结束范围
 */
export function getRandomInterger(end: number, start = 0) {
  const range = end - start;
  const random = Math.floor(Math.random() * range + start);
  return random;
}

/**
 * 计算时间差并匹配收费规则
 * @param {string} inputTime - 输入时间，格式如"2025-01-15 20:14"
 * @param {Array} records - 收费规则记录数组
 * @returns {Object} 匹配到的收费规则项
 */
export function matchTimeDifference(inputTime: string, records: any[]) {
  // 将输入时间转换为Date对象
  const inputDate = new Date(inputTime);
  
  // 计算时间差（毫秒）
  const diffMs = inputDate.getTime() - Date.now();
  // 转换为小时
  const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
  
  // 对records按sorts升序排序（确保顺序正确）
  const sortedRecords = [...records].sort((a, b) => a.sorts - b.sorts);
  
  // 匹配规则
  for (const record of sortedRecords) {
      const name = record.name;
      // 从name中提取时间范围
      const rangeMatch = name.match(/(\d+)到(\d+)小时/);
      
      if (rangeMatch) {
          const minHours = parseInt(rangeMatch[1]);
          const maxHours = parseInt(rangeMatch[2]);
          
          if (diffHours >= minHours && diffHours < maxHours) {
              return {
                  matchedRecord: record,
                  diffHours: diffHours
              };
          }
      }
      // 处理特殊情况（如"24到48小时"）
      else if (name === "24到48小时") {
          if (diffHours >= 24 && diffHours < 48) {
              return {
                  matchedRecord: record,
                  diffHours: diffHours
              };
          }
      }
      // 处理"0到3小时内"的特殊情况
      else if (name === "0到3小时内") {
          if (diffHours >= 0 && diffHours < 3) {
              return {
                  matchedRecord: record,
                  diffHours: diffHours
              };
          }
      }
  }
  
  // 如果没有匹配到任何规则
  return {
      matchedRecord: null,
      diffHours: diffHours,
      message: "未找到匹配的收费规则"
  };
}
